package com.zs4sdk;

import android.content.Context;

import com.common.library.util.ActivityHelper;
import com.zs4sdk.common.Extra;
import com.common.library.data.CommonEventHandler;
import com.common.library.data.EventHandler2Get;
import com.zs4sdk.entity.Zs4GameData;
import com.common.library.util.LogUtil;
import com.common.library.util.ParamsHelper;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 提供给厂商的接口
 * Created by Jancy.Weng on 2015/12/2.
 */
public class Zs4GameTD {

    //初始化SDK
    public static void InitSDK(Context context,int appid,String appkey,String channel){

        if(ActivityHelper.checkChannel(channel)){
            LogUtil.i("Zs4GameTD","Zs4GameTD SDK 初始化");
            ActivityHelper.init(context);
            // app信息
            Zs4GameData.getInstance().setAppId(appid);
            Zs4GameData.getInstance().setAppKey(appkey);
            Zs4GameData.getInstance().setChannel(channel);
            UpLoadDeviceInfo(context);
        }else {
            LogUtil.e("Zs4GameTD","Zs4GameTD SDK 初始化失败,请查看渠道是否正确!");
        }
    }
    //初始化SDK
    public static void InitSDK(Context context,String channel){

        if(ActivityHelper.checkChannel(channel)){
            LogUtil.i("Zs4GameTD","Zs4GameTD SDK 初始化");
            Zs4GameData.getInstance().setChannel(channel);
//            UpLoadDeviceInfo(context);
        }else {
            LogUtil.e("Zs4GameTD","Zs4GameTD SDK 初始化失败,请查看渠道是否正确!");
        }

    }
    /**
     *
     * function: 设置服务器信息
     *
     * @ author:Jancy.Weng
     */
    private static void setUserID(String userID) {

        Zs4GameData.getInstance().setUserId(userID);
    }

    /**
     *
     * function: 设置玩家信息
     *
     * @param playerId
     * @param playerName
     *
     * @ author:Jancy.Weng
     */
    public static void setUserInfo(Context context,String userID,String playerId, String playerName, int playerLevel,String servId, String servName) {

        Zs4GameData.getInstance().setUserId(userID);
        Zs4GameData.getInstance().setPlayerId(playerId);
        Zs4GameData.getInstance().setPlayerName(playerName);
        Zs4GameData.getInstance().setPlayerLevel(String.valueOf(playerLevel));
        Zs4GameData.getInstance().setServId(servId);
        Zs4GameData.getInstance().setServName(servName);
            UpLoadLoginInfo(context);

    }
    /**
     *
     * function: 设置玩家信息
     *
     * @param playerId
     * @param playerName
     *
     * @ author:Jancy.Weng
     */
    private static void setPlayerInfo(String playerId, String playerName, String playerLevel) {
        Zs4GameData.getInstance().setPlayerId(playerId);
        Zs4GameData.getInstance().setPlayerName(playerName);
        Zs4GameData.getInstance().setPlayerLevel(playerLevel);
    }

    /**
     *
     * function: 设置服务器信息
     *
     * @param servId
     * @param servName
     * @ author:Jancy.Weng
     */
    private static void setServerInfo(String servId, String servName) {

    }
    public static void doLogout(){

        Zs4GameData.getInstance().clearLoginInfo();
    }

    /**
     *
     * function: 提交设备报道信息
     *
     * @param ctx
     *  lastUserInfo
     *
     * @ author:Jancy.Weng
     */
    public static void UpLoadDeviceInfo(final Context ctx) {
        if(ActivityHelper.checkChannel(Zs4GameData.getInstance().getChannel())){
            LogUtil.w(ctx.getClass().getSimpleName(), "设备报道");
            new EventHandler2Get(
                    Extra.URL.PHONE_INFO_URL,
                    ParamsHelper.getUserAgent1(ctx),
                    new CommonEventHandler.ResultHandlerInEvent() {

                        @Override
                        public void doResultHanler(JSONObject result) throws JSONException {

                            LogUtil.i("Zs4GameTD","deviceResult:  "+result.toString()+"");
                        }

                        @Override
                        public void doExceptionHandler() {
                        }
                    });
        }else {
            LogUtil.e("Zs4GameTD","Zs4GameTD SDK 初始化失败,请查看渠道是否正确!");
        }
    }
    /**
     *
     * function: 提交设备报道信息
     *
     * @param ctx
     *  lastUserInfo
     *
     * @ author:Jancy.Weng
     */
    private static void UpLoadLoginInfo(final Context ctx) {

        if(ActivityHelper.checkChannel(Zs4GameData.getInstance().getChannel())) {
            LogUtil.w(ctx.getClass().getSimpleName(), "角色报道");
        new EventHandler2Get(
                Extra.URL.LOGIN_INFO_URL,
                ParamsHelper.getUserAgent1(ctx),
                new CommonEventHandler.ResultHandlerInEvent() {

                    @Override
                    public void doResultHanler(JSONObject result) throws JSONException {

                        LogUtil.i("Zs4GameTD","roleResult: "+result.toString()+"");

                    }

                    @Override
                    public void doExceptionHandler() {

                    }
                }); }else {
            LogUtil.e("Zs4GameTD","Zs4GameTD SDK 初始化失败,请查看渠道是否正确!");
        }
    }
    /**
     *
     * function: 提交设备报道信息
     *
     * @param ctx
     *  lastUserInfo
     *
     * @ author:Jancy.Weng
     */
    public static void UpLoadLogoutInfo(final Context ctx) {
        LogUtil.w(ctx.getClass().getSimpleName(), "退出报道");

        if(ActivityHelper.checkChannel(Zs4GameData.getInstance().getChannel())) {
            new EventHandler2Get(
                    Extra.URL.LOGOUT_INFO_URL,
                    ParamsHelper.getUserAgent1(ctx),
                    new CommonEventHandler.ResultHandlerInEvent() {

                        @Override
                        public void doResultHanler(JSONObject result) throws JSONException {

                            LogUtil.i("Zs4GameTD","logoutResult:"+result.toString() + "");
                        }

                        @Override
                        public void doExceptionHandler() {

                        }
                    });
            doLogout();
        }else {
            LogUtil.e("Zs4GameTD","Zs4GameTD SDK 初始化失败,请查看渠道是否正确!");
        }
    }


    public static void setLogShow(boolean isOrNot){
        Zs4GameData.getInstance().setShowLog(isOrNot);

    }
}
